<?php
namespace app\repair\controller;
use think\Request;
use app\common\controller\Common;
use think\Exception;
class Fault extends Common
{
	protected $order = 'fault_id desc';
	protected $afterActionList = [
        'addOperLog'  =>  ['only'=>'del,add,edit'],
    ];

	public function _initialize()
	{
		parent::_initialize();
        $this->model = model('Fault');
	}
	/**
	 * 维修设备、故障描述、外观描述管理
	 */
	public function index($ty = null)
	{

		if(Request()->isGet()){
			if(Request()->isAjax()){
				$data = Request()->param('');
				return $this->getList($data);
			}
			if($ty != null){
				return $this->fetch($ty);
			}
			return $this->fetch();
		}
	}
	/**
	 * 获取子级分类
	 */
	public function sun_cate()
	{
		if(Request()->isPost()){
			if(Request()->isAjax()){
				$data = input('post.');
				$info = $this->model->where('fid',$data['fid'])->select();
				$this->success(config('status.SUCCESS_ONE'),null,$info);
			}
		}
	}
	/**
	 * 批量删除
	 * 维修设备、故障描述、外观描述
	 */
	public function del()
	{
		if(Request()->isAjax()){
			$data = Request()->param('');
			try{
				$this->log_id = implode(',', $data['id']);
				$this->model->startTrans();
				$this->model->del($data['id']);
				$this->model->commit();
				$this->success(config('status.SUCCESS_ONE'));
			}catch(Exception $e){
				$this->model->rollback();
				$this->error(config('status.ERROR_FIVE'));
			}
		}
	}
	/**
	 * 增加
	 * 维修设备、故障描述、外观描述
	 */
	public function add($ty = null)
	{
		if(Request()->isGet()){
			/*分类*/
			if($ty == 0){
				$cate = $this->model->getCate(0);
				$this->assign('cate',$cate);
			}
			$this->assign('str',$this->type_text($ty));
			return $this->fetch('');
		}
		if(Request()->isPost()){
			$data = input('post.');
			$this->log_ty = $data['type'] = $data['ty'];
			$result = $this->model->validate('Fault.add')->allowField(true)->save($data);
			if( $result === false ){
				$this->error($this->model->getError());
			}
			$this->log_id = $this->model->fault_id;
			$this->success(config('status.SUCCESS_ONE'));
		}
	}
	/**
	 * 编辑
	 * 维修设备、故障描述、外观描述
	 */
	public function edit($ids = null)
	{
		if(Request()->isGet()){
			$row = $this->model->get($ids);
			$this->assign('str',$this->type_text($row->type));
			$this->assign('row',$row);
			return $this->fetch('');
		}
		if(Request()->isPost()){
			$data = Request()->param('');
			$this->log_ty = $data['type'] = $data['ty'];
			$this->log_id = $data['ids'];
			$row = $this->model->get($data['ids']);
			if(!$row){
				$this->error(config('status.ERROR_TWO'));
			}
			$result = $row->validate('Fault.edit')->allowField(true)->save($data);
			if( $result === false ){
				$this->error($this->model->getError());
			}
			$this->success(config('status.SUCCESS_ONE'));
		}
	}

	/******************************** 私有 **************************************/

	protected function handleList($data)
	{
		$where = [];
		if(isset($data['ty']) && $data['ty'] != ''){
			$where['type'] = $data['ty'];
		}
		return $where;
	}
	/**
	 * 维修描述
	 */
	private function type_text($type)
	{
		switch ($type) {
			case 0: return '维修设备分类';break;
			case 1: return '维修故障描述';break;
			case 2: return '维修外观描述';break;
			case 3: return '维修设备品牌';break;
			case 4: return '维修设备型号';break;
			default: return '';break;
		}
	}
}